<?php
namespace app\admin\controller;
use app\admin\model\Member;
use think\Cache;
use think\Db;

/**
 * 用户管理
 * Class Member
 */
class MemberController extends BaseController {

    protected function _initialize() {
        parent::_initialize();
    }
	
	/**
     * 合伙人订货管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function pt_dinghuo($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$map['type']=1;
		if(input('status')){
			$map['status'] = input('status');
		}
        $dinghuo_list = db('dinghuo')->where("user_id!=city_id")->where($map)->order($order)->paginate(15, false, get_query());
	    return $this->fetch('pt_dinghuo', ['dinghuo_list' => $dinghuo_list, 'keyword' => $keyword]);
    }
	/**
     * 运营商订货管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function dinghuo2($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$map['type']=2;
		if(input('status')){
			$map['status'] = input('status');
		}

        $dinghuo_list = db('dinghuo')->where("user_id=city_id")->where($map)->order($order)->paginate(15, false, get_query());
	    return $this->fetch('ct_dinghuo', ['dinghuo_list' => $dinghuo_list, 'keyword' => $keyword]);
    }
		//跟新下级会员合伙人id
	public function update_pt($user_id,$ct_id){
		$users=db("member")->where("pid1",$user_id)->field("id,city_id")->select();
		foreach($users as $v){
			db('member')->where('id',$v['id'])->update(['city_id'=>$ct_id]);
			$res=db("member")->where("pid1",$v['id'])->field("id,pid1")->find();
			if($res){
				$this->update_pt($v['id'],$ct_id);
			}
		}
	}
	
	public function city_shenghe($keyword = '', $ziduan = 'user_id', $param='add_time', $sort='desc') {
				if($_POST){
		    		if(input("status")==2){
		    			$sq=db("pt_sq")->where('id',input('id'))->find();
						$user=db("member")->where("id",$sq['user_id'])->find();
						Db::startTrans();
						$result=true;
						$this->update_pt($user['id'],$user['id']);
					
						$parm['is_city'] = 1; 
						$parm['city_id']= $user['id'];
						$res = db('member')->where('id',$user['id'])->update($parm);
						if(!$res){
								$result=false;
						}
						$ct_user=db('member')->where('id',$user['pid1'])->find();
						$old_pt=$user['old_pt'];
						$config =get_config('ticket_config');
						if($ct_user['is_rz']==1&&$ct_user['is_city']==1){
						  gdyj_record_log($ct_user['id'],$config['ct_balance'],"推荐会员成为运营中心奖励",20,order_create_no(),$user['pt_id'],$user['city_id'],$user['id']);								
						}
						$sql="update cz_member set total_yj= total_yj + ".$config['pt_one'].",yj=yj+".$config['pt_one']." where id=".$old_pt ;
						$res=Db::execute($sql);
						if(!$res){
							$result=false;
						}
						$res = db('pt_sq')->where('id',input('id'))->update(['status'=>2]);
						if(!$res){
								$result=false;
						}
					$dt['pay_type']=1;
					$dt['order_type']=8;
					$dt['user_id']=$sq['user_id'];
					$dt['balance']=$config['ct_balance'];
					$dt['sn']=order_create_no();
					$dt['state']=2;
					$dt['add_time']=now_time();
					$dt['pay_time']=now_time();
					$dt['city_id']=$user['city_id'];
					$dt['city_yj']=$config['ct_ct'];
					$res=db('paylog')->insert($dt);
				        if($result){
				        	Db::commit();
							return json(['code'=>200,'msg'=>'操作成功']);
				        }else{
				        	Db::rollback();
				       		return json(['code'=>402,'msg'=>'操作失败']);
						 }
		    		}else{
		    				$result = db('pt_sq')->where('id',input('id'))->update(['status'=>3]);
					        if($result){
								return json(['code'=>200,'msg'=>'操作成功']);
					        }else{
					       		return json(['code'=>402,'msg'=>'操作失败']);
					        }
		    		}
				
      		  }
		        $map = [];
				$order = $param. ' '.$sort;
				$this->assign('param', $param);
				$this->assign('sort', $sort);
		        if ($keyword) {
		            $map['b.'.$ziduan] = $keyword;
		        }
				$this->assign('ziduan', $ziduan);
				$map['a.type'] = 2;
				if(input("status")){
			
				}else{
					$map['a.status'] = 1;
				}
				if(session('city_id')){
					$map['a.city_id'] = session('city_id');
				}
		        $member_list = db('pt_sq')->alias('a')
					->join('member b','a.user_id = b.id','LEFT')
					->field('a.*,b.nick_name as shname,b.sex,b.money')->where($map)->order($order)->paginate(15, false, get_query());
		
				if(input("status")){
				}else{
					$map1['status'] = 1;
				}
				$map1['type'] = 1;
				if(session('city_id')){
					$map1['city_id'] = session('city_id');
				}       
			   	$count = db('pt_sq')->where($map1)->count();
				$this->assign('count', $count);
			    return $this->fetch('city_sh', ['member_list' => $member_list, 'keyword' => $keyword]);
	}
	
	/**
	 * 订货信息
	 */
	public function dinghuo_info(){
		$list = db("dinghuo")->where('id',input("id"))->find();
		$this->assign('dinghuo',$list);
		return $this->fetch();
	}
	
	//操作订货
	public function set_dinghuo(){
    	if(request()->isPost()){
    		$id = input('id');
			$status = input('status');
    		$resutl = db('dinghuo')->where('id',$id)->update(['status'=>$status]);
    		
    		if($resutl){
    			return json(['code'=>200,'msg'=>'操作成功']);
    		}else{
    			return json(['code'=>403,'msg'=>'操作失败']);
    		}
    	}
		return $this->fetch();
	}

    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
 		if ($keyword) {
            $map["a.nick_name|a.real_name|a.mobile"] = ["like","%".$keyword."%"];
        }
		$map['a.is_rz'] = 1;
		$map['a.is_qy']=['neq',1];
		if(session('city_id')){
			$map['a.city_id'] = session('city_id');
		}
		$map['c.state']=2;
        $member_list = db('member')->alias('a')
			->join('member b','a.pid1 = b.id','LEFT')
			->join('cz_paylog c','c.user_id = a.id',"LEFT")
			->field('a.*,b.nick_name as tjname,c.add_time as btime')
			->where($map)->order("btime desc")->paginate(15, false, get_query());
		
		$count = db('member')->where('is_rz',1)->count();
		$this->assign('count', $count);
        return $this->fetch('index', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
	/**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function qy_members($keyword = '', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
 	if ($keyword) {
            $map["a.nick_name|a.real_name|a.mobile"] = ["like","%".$keyword."%"];
        }
		$map['a.is_rz'] = 1;
		$map['a.is_qy']=1;
		if(session('city_id')){
			$map['a.city_id'] = session('city_id');
		}
		$map['c.state']=2;
        $member_list = db('member')->alias('a')
			->join('member b','a.pid1 = b.id','LEFT')
			->join('cz_paylog c','c.user_id = a.id',"LEFT")
			->field('a.*,b.nick_name as tjname,c.add_time as btime')
			->where($map)->order("btime desc")->paginate(15, false, get_query());
		
		$count = db('member')->where('is_rz',1)->count();
		$this->assign('count', $count);
        return $this->fetch('qy_members', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
    /**
     * 添加用户
     * @return mixed
     */
    public function add() {
        return $this->fetch();
    }
	/**
	 * 用户收货地址
	 */
	public function address(){
		$list=db("member_address")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
    /**
     * 保存用户
     */
    public function save() {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
        	$password = $data['password'];
            $data['password'] = md5_salt($password);
			$data['add_time'] = now_time();
			$member_model = new Member();
            if ($member_model->allowField(true)->save($data)) {
                $this->success('添加成功',url("index"));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑用户
     * @param $id
     * @return mixed
     */
    public function edit($id) {
		$member = db('member')->alias('a')
			->join('member b','a.pid1 = b.id','LEFT')
			->join('member c','a.pid2 = c.id','LEFT')
			->field('a.*,b.nick_name as tjname,b.mobile as tjmobile,c.nick_name as tjname1,c.mobile as tjmobile1')
			->where('a.id',$id)->find();
        return $this->fetch('edit', ['member' => $member]);
    }

    /**
     * 更新用户
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            if (!empty($data['password'])) {
                $data['password'] = md5_salt($data['password']);
            }else{
            	$data['password'] = $data['old_pwd'];
            }

			$member_model = new Member();
            if ($member_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                $this->success('更新成功',url("index"));
            } else {
                $this->error('更新失败');
            }
        }
    }
	
	//删除会员
	public function delete_item(){
		$data['status'] = 2;
		if(input("status")){
			$data['status']=input("status");
		}
		$res = db('member')->where('id',input("id"))->update($data);
		if($res){
			 $this->success('操作成功',url("index"));
		}else{
			 $this->success('操作失败');
		}
	}

    /**
     * 合伙人管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function pt_list($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$map['a.is_pt'] = 1;
		if(session('city_id')){
			$map['a.city_id'] = session('city_id');
		}
        $member_list = db('member')->alias('a')
			->join('member b','a.old_pt = b.id','LEFT')
			->field('a.*,b.nick_name as tjname')
			->where($map)->order($order)->paginate(15, false, get_query());
       
	   	$count = db('member')->where('is_pt',1)->count();
		$this->assign('count', $count);
	    return $this->fetch('pt_list', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
	
	 /**
     * 编辑合伙人
     * @param $id
     * @return mixed
     */
    public function ptedit($id) {
    	$member_model = new Member();
        $member = $member_model->where('id',$id)->find();
        return $this->fetch('ptedit', ['member' => $member]);
    }
	 public function ctedit($id) {
    	$member_model = new Member();
        $member = $member_model->where('id',$id)->find();
        return $this->fetch('ctedit', ['member' => $member]);
    }
    /**
     * 更新用户
     * @param $id
     */
    public function ptupdate($id) {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            if (!empty($data['password'])) {
                $data['password'] = md5_salt($data['password']);
            }else{
            	$data['password'] = $data['old_pwd'];
            }

			$member_model = new Member();
            if ($member_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                if(input("is_city")){
                	$this->success('更新成功',url("city_list"));
                }else{
                	$this->success('更新成功',url("pt_list"));
                }	
                
            } else {
                $this->error('更新失败');
            }
        }
    }
	
	//删除会员
	public function delete_ptitem(){
		$data['status'] = 2;
		$res = db('member')->where('id',input("id"))->update($data);
		if($res){
			 $this->success('删除成功',url("index"));
		}else{
			 $this->success('删除失败');
		}
	}
	
	/**
     * 运营中心管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function city_list($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$map['is_city'] = 1; //城市运营中心
        $member_list = db('member')->where($map)->order($order)->paginate(15, false, get_query());
        
       	$count = db('member')->where('is_city',1)->count();
		$this->assign('count', $count);
        return $this->fetch('city_list', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
	
	/**
     * 运营中心审核管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function citysh_list($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
	    if ($keyword) {
            $map['b.'.$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$map['a.type'] = 2;
		$map['a.status'] = 1;
        $member_list = db('pt_sq')->alias('a')
			->join('member b','a.user_id = b.id','LEFT')
			->field('a.*,b.nick_name as shname,b.sex,b.money')->where($map)->order($order)->paginate(15, false, get_query());
	   
	    return $this->fetch('citysh_list', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
	
	//运营中心审核
	public function shenghe() {
    	if($_POST){
    		// type 1 通过  2 驳回    
    		if(input('type')==1){
    			$parm['status'] = 2;
				$map['is_city']= 1;
				
				$userinfo = db('member')->where('id',input('user_id'))->find();
				$data['login_name'] = $userinfo['mobile'];
				$data['city_id'] = input('user_id'); 
			 	$data['login_pwd'] = md5_salt('123456');
				$data['add_time'] = now_time(); 
    		}else{
    			$parm['status'] = 3;
				$map['is_city']= 4;
    		}
			$result = db('pt_sq')->where('id',input('id'))->update($parm);
						
			$res = db('member')->where('id',input('user_id'))->update($map);			
	        if($result){
	        	if(input('type')==1){
	        		db("user")->insert($data);
					$user_id = db('user')->getLastInsID();
					$data1['uid'] = $user_id;
					$data1['group_id'] = 12;
					db("auth_group_access")->insert($data1);
	        	}        	
				return json(['code'=>200,'msg'=>'操作成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'操作失败']);
	        }
        }
	}
	
	//运营中心审核驳回
	public function bohui() {
    	if($_POST){
       	 	$parm['is_city'] = 4;  
       	 	$result = db('member')->where('id',input('id'))->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'操作成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'操作失败']);
	        }
        }
	}
	
	 /**
     * 推广配置
     */
    public function config() {
		if ($this->request->isPost()) {
            $ticket_config   = $this->request->post('ticket_config/a');
            $data['value'] = serialize($ticket_config);
            if (Db::name('system')->where('name', 'ticket_config')->update($data) !== false) {
                Cache::set('ticket_config', $ticket_config);
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }else{
        	if (Cache::has('')) {
	            $ticket_config = Cache::get('ticket_config');
	        } else {
	            $ticket_config = Db::name('system')->field('value')->where('name', 'ticket_config')->find();
	            $ticket_config = unserialize($ticket_config['value']);
	        }
        	return $this->fetch('config', ['ticket_config' => $ticket_config]);
        }
    }
	
	/**
     * 注册会员管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function regist_list($keyword = '',$param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map["a.nick_name|a.real_name|a.mobile"] = ["like","%".$keyword."%"];
        }
		
		$map['a.is_rz'] = 2;
		$map['a.status']=1;
		if(session('city_id')){
			$map['a.city_id'] = session('city_id');
		}
        $member_list = db('member')->alias('a')
			->join('member b','a.pid1 = b.id','LEFT')
			->field('a.*,b.nick_name as tjname')
			->where($map)->order($order)->paginate(15, false, get_query());
			
		 $count = db('member')->where('is_rz',2)->count();
		 $this->assign('count', $count);
        return $this->fetch('regist_list', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
	
	/**
     * 注册会员升级
     * @param $id
     */
    public function zcedit($id) {
		$data['is_rz'] = 1;
		$data['rz_type'] = 2;
		$res = db('member')->where('id',input("id"))->update($data);
		if($res){
			 $this->success('操作成功',url("regist_list"));
		}else{
			 $this->success('操作失败');
		}
    }
	
	 /**
     * 访问用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function visit_list($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$where = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		
		if(input('begin_time') && input('end_time')){
			$map['add_time'] = ['between time',[input('begin_time'),input('end_time')]];
			$this->assign('begin_time', input('begin_time'));
			$this->assign('end_time', input('end_time'));
		}elseif(input("begin_time")){
			$map['add_time']=['>=',"".input("begin_time").""];
			$this->assign('begin_time',input("begin_time"));
			$this->assign('end_time','');			
		}elseif(input("end_time")){
			$map['add_time']=['<=',"".input("end_time").""];
			$this->assign('begin_time','');		
			$this->assign('end_time',input("end_time"));			
		}else{
			$this->assign('begin_time','');	
			$this->assign('end_time','');		
		}
		
		$admin_id = session('ADMIN')['id'];
		if($admin_id!=1){
			$map['vuser_id'] = $admin_id;
			$where['vuser_id'] = $admin_id;
		}	
        $visit_list = db('visit')->where($map)->order($order)->paginate(15, false, get_query());
			
		$count = db('visit')->where($where)->count();
		$tc=db('visit')->wheretime('add_time',"today")->count();
		$this->assign('tc',$tc);
		
		$mc=db('visit')->wheretime('add_time',"month")->count();
		$this->assign('mc',$mc);
		$this->assign('count', $count);
        return $this->fetch('visit_list', ['visit_list' => $visit_list, 'keyword' => $keyword]);
    }
	
	//删除访问者
	public function delete_visit(){
		$res = del_data("visit",input("id"));
		if($res){
			 $this->success('删除成功',url("visit_list"));
		}else{
			 $this->success('删除失败');
		}
	}
	
	/**
     * 合伙人审核管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function ptsh_list($keyword = '', $ziduan = 'user_id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map['b.'.$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$map['a.type'] = 1;
		if(input("status")){
			
		}else{
			$map['a.status'] = 1;
		}
		if(session('city_id')){
			$map['a.city_id'] = session('city_id');
		}
        $member_list = db('pt_sq')->alias('a')
			->join('member b','a.user_id = b.id','LEFT')
			->field('a.*,b.nick_name as shname,b.sex,b.money')->where($map)->order($order)->paginate(15, false, get_query());
		if(input("status")){
			
		}else{
			$map1['status'] = 1;
		}
		
		$map1['type'] = 1;
		if(session('city_id')){
			$map1['city_id'] = session('city_id');
		}       
	   	$count = db('pt_sq')->where($map1)->count();
		$this->assign('count', $count);
	    return $this->fetch('ptsh_list', ['member_list' => $member_list, 'keyword' => $keyword]);
    }
	
	//合伙人审核
	public function ptshenghe() {
    	if($_POST){
    		// type 1 通过  2 驳回    
    		if(input('type')==1){
    			$parm['status'] = 2;
				$map['is_pt']= 1;
    		}else{
    			$parm['status'] = 3;
				$map['is_pt']= 4;
    		}
			$result = db('pt_sq')->where('id',input('id'))->update($parm);
			
			
			$res = db('member')->where('id',input('user_id'))->update($map);
	        if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'操作失败']);
	        }
        }
	}
	
	//合伙人审核驳回
	public function ptbohui() {
    	if($_POST){
       	 	$parm['is_pt'] = 4;  
       	 	$result = db('member')->where('id',input('id'))->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'操作成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'操作失败']);
	        }
        }
	}
	
	//会员卡设置
	public function cardset(){
		if ($this->request->isPost()) {
            $ticket_config   = $this->request->post('card_config/a');
            $data['value'] = serialize($ticket_config);
            if (Db::name('system')->where('name', 'card_config')->update($data) !== false) {
                Cache::set('card_config', $ticket_config);
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }else{
        	if (Cache::has('')) {
	            $ticket_config = Cache::get('card_config');
	        } else {
	            $ticket_config = Db::name('system')->field('value')->where('name', 'card_config')->find();
	            $ticket_config = unserialize($ticket_config['value']);
	        }
        	return $this->fetch('cardset', ['card_config' => $ticket_config]);
        }
	}
	
	/**
     * 会员升级企业会员
     * @param $id
     */
    public function hyedit($id) {
		$data['is_qy'] = 1;
		$data['rz_type'] = 2;
		$res = db('member')->where('id',input("id"))->update($data);
		if($res){
			 $this->success('操作成功',url("regist_list"));
		}else{
			 $this->success('操作失败');
		}
    }

    public function pt1edit($id) {
		$data['is_pt'] = 1;
		$data['pt_id']=input("id");
		$res = db('member')->where('id',input("id"))->update($data);
		if($res){
			$this->update_pt1(input("id"), input("id"));
		}
		if($res){
			 $this->success('操作成功');
		}else{
			 $this->success('操作失败');
		}
    }
	 public function ct1edit($id) {
		$data['is_pt'] = 1;
		$data['pt_id']=input("id");
		$data['is_city'] = 1;
		$data['city_id']=input("id");
		$res = db('member')->where('id',input("id"))->update($data);
		if($res){
			$this->update_ct1(input("id"), input("id"));
		}
		if($res){
			 $this->success('操作成功');
		}else{
			 $this->success('操作失败');
		}
    }
	 //跟新下级会员合伙人id
	public function update_pt1($user_id,$pt_id){
		$users=db("member")->where("pid1",$user_id)->field("id,pt_id")->select();
		foreach($users as $v){
			db('member')->where('id',$v['id'])->update(['pt_id'=>$pt_id]);
			$res=db("member")->where("pid1",$v['id'])->field("id,pid1")->find();
			if($res){
				$this->update_pt1($v['id'],$pt_id);
			}
		}
		
	}
	public function update_ct1($user_id,$pt_id){
		$users=db("member")->where("pid1",$user_id)->field("id,pt_id,city_id")->select();
		foreach($users as $v){
			db('member')->where('id',$v['id'])->update(['pt_id'=>$pt_id,'city_id'=>$pt_id]);
			$res=db("member")->where("pid1",$v['id'])->field("id,pid1")->find();
			if($res){
				$this->update_ct1($v['id'],$pt_id);
			}
		}
		
	}
}
